package com.quasar.hpatient.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.utils.FileUtils;
import com.quasar.hpatient.utils.QrCodeUtils;

import java.io.File;


public class MyQrodeDialog extends Dialog {

    private ImageView mIvCode;
    private Bitmap bitmap;
    PatientInfoData patientInfoData= MainAppliaction.getInstance().getPatient();

    private MyQrodeDialog(Context context, boolean flag,
                          OnCancelListener listener) {
        super(context, flag, listener);
    }

    @SuppressLint("InflateParams")
    private MyQrodeDialog(final Context context, int defStyle) {
        super(context, defStyle);
        View contentView = getLayoutInflater().inflate(
                R.layout.dialog_my_qr_code, null);
        mIvCode = (ImageView) contentView.findViewById(R.id.iv_qr_code);

        /**
         * 将用户的手机号码转换成二维码
         */
        try {
            String phone= null;
            if(patientInfoData.getPhone()!=null && patientInfoData.getPhone().length()>0){
                phone=patientInfoData.getPhone();
            }else{
                phone=patientInfoData.getName();
            }
            bitmap = QrCodeUtils.Create2DCode(String.format(""+phone));
            mIvCode.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);

//        mIvCode.setOnLongClickListener(new OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                dismiss();
//                if (FileUtils.bitmapToFile(bitmap,
//                        FileUtils.getSavePath("helpuser") + "/myqrcode.png")) {
//                  showToast("二维码已保存到helpuser文件夹下");
//                    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(FileUtils.getSavePath("helpuser") + "/myqrcode.png"))));
//                } else {
//                   showToast("SD卡不可写，二维码保存失败");
//                }
//                return false;
//            }
//        });
//
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                MyQrodeDialog.this.dismiss();
                return false;
            }
        });
        super.setContentView(contentView);
    }

    private void showToast(String s) {
        Toast.makeText(getContext(),s,Toast.LENGTH_SHORT).show();
    }

    public MyQrodeDialog(Context context) {
        this(context, R.style.quick_option_dialog);
    }


    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.CENTER);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = ViewGroup.LayoutParams.MATCH_PARENT;
        p.height = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(p);
    }
}
